package c.j.a.c.g$d;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import c.j.a.a.a.a.g;
import c.j.a.c.g$g.d.c;
import c.j.a.c.g.y;
import c.j.a.c.q.d;
import c.j.a.c.z;
import com.ss.android.downloadlib.activity.TTDelegateActivity;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import org.json.JSONObject;

/* compiled from: AdDownloadDialogManager */
public class b {
    public static b e;
    public CopyOnWriteArrayList<c.j.a.c.g$g.a> a;
    public boolean b = false;
    public String c;
    public c d;

    /* compiled from: AdDownloadDialogManager */
    public interface a {
        void a();
    }

    public b() {
        c cVar = new c();
        this.d = cVar;
        String str = "sp_ad_install_back_dialog";
        String str2 = "key_uninstalled_list";
        Objects.requireNonNull(cVar);
        CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList();
        try {
            String string = y.a().getSharedPreferences(str, 0).getString(str2, "");
            if (!TextUtils.isEmpty(string)) {
                JSONObject jSONObject = new JSONObject(string);
                Iterator keys = jSONObject.keys();
                while (keys.hasNext()) {
                    c.j.a.c.g$g.a a = c.j.a.c.g$g.a.a(jSONObject.optJSONObject((String) keys.next()));
                    if (a != null) {
                        copyOnWriteArrayList.add(a);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.a = copyOnWriteArrayList;
    }

    public static b a() {
        if (e == null) {
            e = new b();
        }
        return e;
    }

    public void b(long j) {
        if (y.t().optInt("enable_open_app_dialog", 0) == 1) {
            int i = TTDelegateActivity.b;
            Intent intent = new Intent(y.a(), TTDelegateActivity.class);
            intent.addFlags(268435456);
            intent.putExtra("type", 4);
            intent.putExtra("model_id", j);
            if (y.a() != null) {
                y.a().startActivity(intent);
            }
        }
    }

    public void c(long j, long j2, long j3, String str, String str2, String str3, String str4) {
        int i = 0;
        while (true) {
            String str5 = "key_uninstalled_list";
            String str6 = "sp_ad_install_back_dialog";
            if (i < this.a.size()) {
                c.j.a.c.g$g.a aVar = (c.j.a.c.g$g.a) this.a.get(i);
                if (aVar == null || aVar.b != j2) {
                    i++;
                } else {
                    this.a.set(i, new c.j.a.c.g$g.a(j, j2, j3, str, str2, str3, str4));
                    this.d.a(str6, str5, this.a);
                    return;
                }
            }
            this.a.add(new c.j.a.c.g$g.a(j, j2, j3, str, str2, str3, str4));
            this.d.a(str6, str5, this.a);
            return;
        }
    }

    public void d(Context context, c.j.a.c.g$g.a aVar, boolean z, a aVar2) {
        String str;
        this.a.clear();
        c.j.a.b.a.c.a g = c.a.g(aVar.b);
        if (g == null) {
            d.n();
        } else {
            g q = y.q();
            c.j.a.a.a.e.c.b bVar = new c.j.a.a.a.e.c.b(context);
            bVar.b = z ? "应用安装确认" : "退出确认";
            Object[] objArr = new Object[1];
            objArr[0] = TextUtils.isEmpty(aVar.e) ? "刚刚下载的应用" : aVar.e;
            bVar.c = String.format("%1$s下载完成，是否立即安装？", objArr);
            bVar.d = "立即安装";
            if (z) {
                str = "暂不安装";
            } else {
                str = String.format("退出%1$s", new Object[]{context.getResources().getString(context.getApplicationContext().getApplicationInfo().labelRes)});
            }
            bVar.e = str;
            bVar.f = false;
            str = aVar.g;
            Drawable drawable = null;
            if (!(context == null || TextUtils.isEmpty(str))) {
                PackageManager packageManager = context.getPackageManager();
                PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(str, 0);
                if (packageArchiveInfo != null) {
                    ApplicationInfo applicationInfo = packageArchiveInfo.applicationInfo;
                    applicationInfo.sourceDir = str;
                    applicationInfo.publicSourceDir = str;
                    try {
                        drawable = applicationInfo.loadIcon(packageManager);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            bVar.g = drawable;
            bVar.h = new a(this, g, context, aVar, aVar2);
            bVar.i = 1;
            q.b(bVar.a());
            c.j.a.c.o.b.b.a.i("backdialog_show", g);
            this.c = aVar.d;
        }
        this.b = true;
        z a = z.a(context);
        Objects.requireNonNull(a);
        a.e = System.currentTimeMillis();
        String str2 = "sp_ad_install_back_dialog";
        str = "key_uninstalled_list";
        Objects.requireNonNull(this.d);
        if (!TextUtils.isEmpty(str2) && !TextUtils.isEmpty(str)) {
            y.a().getSharedPreferences(str2, 0).edit().putString(str, "").apply();
        }
    }

    public void e(String str) {
        String str2 = "";
        if (TextUtils.isEmpty(str)) {
            this.c = str2;
        } else if (TextUtils.equals(this.c, str)) {
            this.c = str2;
        }
    }
}
